;
(function () {
	new Vue({
		el: '#accounting_subject_config',
		data: {
			accountingArr: [],
			accountingSelectId: '',
			accountingSubjectArr: [],
			data: {},
			saveFlag: false,
			ops: {
				bar: {
					background: '#c1c1c1'
				}
			},

			deepList: [],
			FZHSList: [],
			XJLList: [],

			dialogType: '',
			dialogFlag: false,
			dialogIndex: '',

			addEditObj: {},
		},
		methods: {
			// 数组对象显示文本
			getArrText(arr) {
				if (!arr) return '--'
				let t = []
				arr.map(item => {
					t.push(item.name)
				})
				return t + ''
			},

			//获取账套列表
			accountingsDt() {
				hlhttp.get({
					url: 'acct/accountings',
					success: (data) => {
						if (!data.length) return;
						this.accountingArr = data;
						this.accountingSelectId = sessionStorage.getItem("accountId");
						this.accountingSubjectsDt()
						this.getXJLlist()
						this.getFZHSlist()
					}
				})
			},
			//选择账套
			selectAccounting(item) {
				this.accountingSelectId = item.id;
				sessionStorage.setItem("accountId", this.accountingSelectId);
				this.accountingSubjectsDt()
				this.getXJLlist()
				this.getFZHSlist()
			},
			//获取会计科目列表
			accountingSubjectsDt() {
				hlhttp.get({
					url: 'acct/accounting_subjects',
					data: {
						accounting_id: this.accountingSelectId
					},
					success: (data) => {
						this.accountingSubjectArr = data
					}
				})
			},
			// 科目编码内边距
			getPd(val) {
				let num = (this.deepList.indexOf(val) + 1) * 10
				return num + 'px'
			},
			// 获取辅助核算列表
			getFZHSlist() {
				let data = {
					cash_flag: 'N',
					accounting_id: this.accountingSelectId + ''
				}
				hlhttp.get({
					url: 'acct/acct_assist/manage_list',
					data: data,
					success: res => {
						res.map(item => {
							item.code = item.id
							item.name = item.assistName
						})
						this.FZHSList = res
					}
				})
			},
			// 获取现金流列表
			getXJLlist() {
				let data = {
					cash_flag: 'Y',
					accounting_id: this.accountingSelectId + ''
				}
				hlhttp.get({
					url: 'acct/acct_assist/manage_list',
					data: data,
					success: res => {
						res.map(item => {
							item.code = item.id
							item.name = item.assistName
						})
						this.XJLList = res
					}
				})
			},
			//新增一级OR子级
			addItem(val, data, index) {
				// val为1新增一级 2位新增子级,3为编辑 ()
				this.dialogFlag = true
				this.dialogType = val
				this.dialogIndex = index
				if (val == '1') {
					this.addEditObj = {
						id: '',
						code: '',
						name: '',
						assists: [],
						cashes: [],
						deep: 'A',
						parentCode: '',
						parentId: '#',
					}
				}

				if (val == '2') {
					this.addEditObj = {
						id: '',
						code: data.code,
						name: '',
						assists: [],
						cashes: [],
						deep: '',
						parentCode: data.code,
						parentId: data.id,
					}
				}
				if (val == 3) {
					this.addEditObj = {
						id: data.id,
						code: data.code,
						name: data.name,
						assists: [],
						cashes: [],
						deep: data.deep,
						parentCode: data.parentCode || '',
						parentId: data.parentId || '',
					}
					if (data.assists && data.assists.length) {
						data.assists.map(item => {
							this.addEditObj.assists.push(item.id)
						})
					}
					if (data.cashes && data.cashes.length) {
						data.cashes.map(item => {
							this.addEditObj.cashes.push(item.id)
						})
					}
				}
			},
			//删除会计科目
			delAccountingSubject(id) {
				hllayer.confirm('删除科目时将连同子科目一同删除，且不可恢复', () => {
					hlhttp.delete({
						url: 'acct/accounting_subject?id=' + id,
						success: () => {
							this.accountingSubjectsDt()
						}
					})
				})
			},
			//科目数字验证
			validateCodeIsNumber() {
				if (isNaN(this.addEditObj.code)) {
					this.addEditObj.code = ''
					hl.toast('科目编码为数字')
				}
			},
			//科目编码前缀不可改变验证
			inputValidateCode() {
				if (this.addEditObj.parentCode) {
					let code = JSON.parse(JSON.stringify(this.addEditObj.code)).split('');
					let parentCode = this.addEditObj.parentCode.split('');
					for (let i = 0; i < parentCode.length; i++) {
						code[i] = parentCode[i]
					}
					this.addEditObj.code = code.toString().replace(/,/igm, '')
					if (this.addEditObj.code.length < this.addEditObj.parentCode.length) {
						this.addEditObj.code.code = JSON.parse(JSON.stringify(this.addEditObj.code.parentCode))
						return
					}
				}
			},
			// 校验是否填写完整
			saveCheckNameCode() {
				if (!this.addEditObj.code) {
					hl.toast('请输入科目编码')
					return false
				}
				if (!this.addEditObj.name) {
					hl.toast('请输入科目名称')
					return false
				}
				return true
			},
			// 保存前的校验（名称，编码是否重复）
			saveCheckNameCodeRepeat() {
				let flag = true
				let arr = JSON.parse(JSON.stringify(this.accountingSubjectArr))
				if (this.dialogType == '3') {
					arr.splice(this.dialogIndex, 1)
				}
				arr.map((item, index) => {
					if (item.code == this.addEditObj.code || item.name == this.addEditObj.name) {
						flag = false
					}
				})
				return flag
			},
			//保存科目编码
			saveCode() {
				if (!this.saveCheckNameCode()) return
				if (!this.saveCheckNameCodeRepeat()) {
					hl.toast('科目编码、科目名称不可重复')
					return
				}
				if (this.addEditObj.id) {
					this.editAccountingSubject()
				}
				if (!this.addEditObj.id) {
					this.addAccountingSubject()
				}
			},
			//新增会计科目
			addAccountingSubject() {
				hlhttp.post({
					url: 'acct/accounting_subject',
					data: {
						pid: this.addEditObj.parentId || '#',
						code: this.addEditObj.code,
						name: this.addEditObj.name,
						accounting_id: this.accountingSelectId,
						assists: this.addEditObj.assists + '',
						cashes: this.addEditObj.cashes + '',
					},
					success: () => {
						this.dialogFlag = false
						hl.toast('新增成功')
						this.accountingSubjectsDt()
					}
				})
			},
			//编辑会计科目
			editAccountingSubject() {
				this.dialogFlag = false
				hllayer.confirm1('子科目编码将跟随上级科目发生变动，<br/>保存后新生产的凭证生效', () => {
					hlhttp.put({
						url: 'acct/accounting_subject',
						data: {
							id: this.addEditObj.id,
							code: this.addEditObj.code,
							name: this.addEditObj.name,
							accounting_id: this.accountingSelectId,
							assists: this.addEditObj.assists + '',
							cashes: this.addEditObj.cashes + '',
						},
						success: () => {
							hl.toast('编辑成功')
							this.accountingSubjectsDt()
						}
					})
				})
			},
		},
		created() {
			var count1 = 0
			var arr1 = []
			for (var i = 65; i < 91; i++) {
				arr1[count1] = String.fromCharCode(i)
				count1++
			}
			this.deepList = arr1
		},
		mounted() {
			this.accountingsDt()
		}
	})
}())